package com.prs.crm.action.purchase;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.purchase.PaymentGather;
import com.prs.crm.domain.purchase.PaymentOrder;
import com.prs.crm.domain.purchase.PurchaseOrder;
import com.prs.crm.domain.purchase.PurchaseOrderDetail;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.service.purchase.PaymentGatherService;
import com.prs.crm.service.purchase.PaymentOrderService;
import com.prs.crm.service.purchase.PurchaseOrderService;

/**
 * @author 兰小刚
 * @version 创建时间：2010-7-14 下午02:45:10 类说明
 */
public class PaymentOrderAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private PaymentOrder paymentOrder;

	private PurchaseOrder purchaseOrder;

	private PaymentGather paymentGather;

	private PaymentGatherService paymentGatherService;

	private PaymentOrderService paymentOrderService;

	private PurchaseOrderService purchaseOrderService;

	private String[] paymentOrderMonys;
	private Product[] products;

	private double allMony = 0.0;

	private PaginationSupport pagination;
	private Integer[] productNums;

	@Clew(results = { "success", "none" })
	@Action(value = "savePurchaseOrderPayment", results = {
			@Result(name = "success", type = "redirect", location = "listPaymentOrder"),
			@Result(name = "error", type = "chain", location = "editPurchaseOrderToPayment"),
			@Result(name = "none", type = "redirect", location = "listPaymentOrder") })
	public String savePurchaseOrderPayment() {
		if (paymentOrder.getPayName() == null
				|| paymentOrder.getPayName().equals("")) {
			this.addActionError("采购主题不能为空");
			return ERROR;
		}
		if (paymentOrder.getPayCode() == null
				|| paymentOrder.getPayCode().equals("")) {
			this.addActionError("采购编号不能为空");
			return ERROR;
		}
		if (products == null||productNums==null) {
			this.addActionError("商品不能为空");
			return ERROR;
		}
		purchaseOrder = this.getPurchaseOrderService().get(
				purchaseOrder.getId());
		for (int i = 0; i < products.length; i++) {
			
			allMony = allMony + Double.parseDouble(paymentOrderMonys[i]);
			for (PurchaseOrderDetail detail : purchaseOrder.getDetails()) {
				if (detail.getProduct() == products[i]) {
					detail.setPayNum(detail.getPayNum() + productNums[i]);
					if (detail.getPayNum() == detail.getStockNum()) {
						detail.setPayState(1);
					}
				}
			}
		}
		paymentOrder.setPayMony(allMony);
		paymentOrder.setArealymony(0.0);
		paymentOrder.setPatState(1);
		paymentOrder.setPurchaseOrder(purchaseOrder);
		this.getPaymentOrderService().save(paymentOrder);
		boolean a = true;
		for (PurchaseOrderDetail detail : purchaseOrder.getDetails()) {
			if (detail.getPayState()== 0) {
				a = false;
				break;
			}
		}
		if (a) {
			purchaseOrder.setPurPayState(2);
		}
		
		this.getPurchaseOrderService().saveOrUpdate(purchaseOrder);

		this.setPrompt("提交成功");
		return SUCCESS;
	}

	@Action("listPaymentOrder")
	public String excute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				PaymentOrder.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPaymentOrderService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	public PaymentOrder getPaymentOrder() {
		return paymentOrder;
	}

	public void setPaymentOrder(PaymentOrder paymentOrder) {
		this.paymentOrder = paymentOrder;
	}

	public PaymentOrderService getPaymentOrderService() {
		return paymentOrderService;
	}

	public void setPaymentOrderService(PaymentOrderService paymentOrderService) {
		this.paymentOrderService = paymentOrderService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public String[] getPaymentOrderMonys() {
		return paymentOrderMonys;
	}

	public void setPaymentOrderMonys(String[] paymentOrderMonys) {
		this.paymentOrderMonys = paymentOrderMonys;
	}

	public PaymentGather getPaymentGather() {
		return paymentGather;
	}

	public void setPaymentGather(PaymentGather paymentGather) {
		this.paymentGather = paymentGather;
	}

	public double getAllMony() {
		return allMony;
	}

	public void setAllMony(double allMony) {
		this.allMony = allMony;
	}

	public PaymentGatherService getPaymentGatherService() {
		return paymentGatherService;
	}

	public void setPaymentGatherService(
			PaymentGatherService paymentGatherService) {
		this.paymentGatherService = paymentGatherService;
	}

	public PurchaseOrder getPurchaseOrder() {
		return purchaseOrder;
	}

	public void setPurchaseOrder(PurchaseOrder purchaseOrder) {
		this.purchaseOrder = purchaseOrder;
	}

	public PurchaseOrderService getPurchaseOrderService() {
		return purchaseOrderService;
	}

	public void setPurchaseOrderService(
			PurchaseOrderService purchaseOrderService) {
		this.purchaseOrderService = purchaseOrderService;
	}

	public Integer[] getProductNums() {
		return productNums;
	}

	public void setProductNums(Integer[] productNums) {
		this.productNums = productNums;
	}

	public Product[] getProducts() {
		return products;
	}

	public void setProducts(Product[] products) {
		this.products = products;
	}

}
